package com.material.common.utils;

import com.aliyun.credentials.exception.CredentialException;
import com.aliyun.credentials.models.Config;
import com.aliyun.ocr20191230.Client;
import com.aliyun.ocr20191230.models.RecognizeLicensePlateRequest;
import com.aliyun.ocr20191230.models.RecognizeLicensePlateResponse;
import com.aliyun.ocr20191230.models.RecognizeLicensePlateResponseBody;
import com.aliyun.teautil.models.RuntimeOptions;
import com.material.common.config.AliOssProperties;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * @author ganxinge
 */
public class OcrUtil {


    private static Logger LOG = LoggerFactory.getLogger(OcrUtil.class);

    public static void main(String[] args) {
        String imageUrl = "https://smart-fuel.oss-cn-hangzhou.aliyuncs.com/2025/06/09/20250609141903.jpg";
        String s = ocrCar(imageUrl);
        System.out.println(s);
    }
    public static String ocrCar(String image){
        Client client = createClient();
        if(client == null){
            return null;
        }
        RecognizeLicensePlateRequest request = new RecognizeLicensePlateRequest();
        request.setImageURL(image);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            RecognizeLicensePlateResponse response = client.recognizeLicensePlateWithOptions(request, runtime);
            RecognizeLicensePlateResponseBody body = response.getBody();
            RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyData data = body.getData();
            List<RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyDataPlates> plates = data.getPlates();
            if(CollectionUtils.isEmpty(plates)){
                return null;
            }
            RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyDataPlates recognizeLicensePlateResponseBodyDataPlates = plates.get(0);
            String plateNumber = recognizeLicensePlateResponseBodyDataPlates.getPlateNumber();
            return plateNumber;
        } catch (Exception e) {
            LOG.error("识别车牌异常",e);
        }
        return null;
    }

    public static Client createClient()  {
        // 工程代码建议使用更安全的无AK方式，凭据配置方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.credentials.Client credential = null;
        try {
            Config credentialConfig = new Config();
            credentialConfig.accessKeyId = AliOssProperties.ACCESS_KEY_ID;
            credentialConfig.accessKeySecret = AliOssProperties.ACCESS_KEY_SECRET;
            credentialConfig.type= "access_key";
            credential = new com.aliyun.credentials.Client(credentialConfig);
            com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
            config.setCredential(credential);
            // Endpoint 请参考 https://api.aliyun.com/product/ocr
            config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
            return new Client(config);
        } catch (ParseException e) {
            LOG.error("createClient parse error",e);
        } catch (CredentialException e) {
            LOG.error("createClient client error",e);
        } catch (IOException e) {
            LOG.error("createClient io error",e);
        } catch (Exception e) {
            LOG.error("createClient error",e);
        }
        return null;
    }
}
